<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>字典新增/编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../assets/layui/css/layui.css" media="all">
</head>

<body>
<fieldset class="layui-elem-field layui-field-title" style="padding-top: 5px;border: hidden">
    <form class="layui-form" action="" lay-filter="dataForm">
        <input type="hidden" name="id">
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <label class="layui-form-label"><span style="color: red;">*</span>字典类型</label>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <select id="ddlDictType" name="dictType" lay-filter="dictType">
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <label class="layui-form-label"><span style="color: red;">*</span>字典Key</label>
            <div class="layui-input-block">
                <input type="text" id="iptdictKey" name="dictKey" lay-verify="required" required placeholder="字典Key"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <label class="layui-form-label"><span style="color: red;">*</span>字典值</label>
            <div class="layui-input-block">
                <input type="text" id="iptdictValue" name="dictValue" lay-verify="required" required placeholder="字典值"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <div class="layui-input-block" style="text-align: center;">
                <button class="layui-btn" lay-submit="" lay-filter="formSubmit">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</fieldset>
<script src="../assets/layui/layui.js"></script>
<script>
    layui.config({
        base: '../js/'
    })
    layui.use(['jquery','form','validate','common'], function () {
        var form = layui.form,
            $ = layui.jquery,
            layer = layui.layer,
            validate = layui.validate,
            common = layui.common;
        validate.regVerify();//注册验证
        var id = common.getUrlParam("id");
        var type = common.getUrlParam("type");

        if (type=="edit"){
            //加载数据
            $("#ddlDictType").attr("readonly","readonly");
        }
        //初始化下拉框
        common.initSelectData("#ddlDictType","common/list4Select?type=DDType",function() {
            if(type=='edit'){
                //获取用户信息
                $.ajax({
                    url: "dict/info",
                    dataType: "json",
                    data: { id: id },
                    success: function (rtn) {
                        if (rtn.code===0) {
                            form.val('dataForm',{
                                id:rtn.data.id,
                                dictKey:rtn.data.dictKey,
                                dictValue:rtn.data.dictValue,
                                dictType:rtn.data.dictType
                            });
                        }
                    }
                });
            }
            form.render();//刷新
        });


        //监听提交
        form.on('submit(formSubmit)', function (data) {
            //common.consoleDebug(JSON.stringify(data.field));
            data.field.parentKey="00";
            $.ajax({
                type: 'post',
                url: 'dict/'+(type=="edit"?"edit":"add"),
                data: data.field,
                success: function(rtn){
                    if (rtn.code===0) {
                        common.showInfo('处理成功!',function(){
                            //刷新前页
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.document.getElementById('btnSearch').click();
                            parent.layer.close(index);
                        });
                    }
                    else{
                        common.showError(rtn.msg);
                    }
                },
                error: function(XmlHttpRequest, textStatus, errorThrown){
                    common.showError('保存错误');
                }
            });
            return false;
        });
    });
</script>
</body>
</html>